package com.gwt.server.domain;

import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import com.gwt.server.BeerListService;

public class Beer {
	/**
	 * New instances could be created on the client, but it's a better demo to
	 * send back a Beer with a bunch of dummy data.
	 */
	public static Beer createBeer() {
		return BeerListService.createBeer();
	}

	/**
	 * The RequestFactory requires a static finder method for each proxied type.
	 * Soon it should allow you to customize how instances are found.
	 */
	public static Beer findBeer(String id) {
		return BeerListService.findBeer(id);
	}

	@NotNull
	private final BeerStyle style = new BeerStyle();

	@NotNull
	private String description = "DESC";

	@NotNull
	@Size(min = 2, message = "Beers aren't just characters")
	private String name;

	// May be null if the person is newly-created
	private String id;

	@NotNull
	@DecimalMin("0")
	private Integer version = 0;

	private String note;

	public Beer() {
	}

	protected Beer(Beer copyFrom) {
		copyFrom(copyFrom);
	}

	public void copyFrom(Beer copyFrom) {
		style.copyFrom(copyFrom.style);
		description = copyFrom.description;
		name = copyFrom.name;
		id = copyFrom.id;
		version = copyFrom.version;
		note = copyFrom.note;
	}

	public BeerStyle getStyle() {
		return style;
	}

	public String getDescription() {
		return description;
	}

	/**
	 * The RequestFactory requires a Long id property for each proxied type.
	 * <p>
	 * The requirement for some kind of id object with proper hash / equals
	 * semantics is not going away, but it should become possible to use types
	 * other than Long, and properties other than "id".
	 */
	public String getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getNote() {
		return note;
	}

	/**
	 * The RequestFactory requires an Integer version property for each proxied
	 * type, but makes no good use of it. This requirement will be removed soon.
	 */
	public Integer getVersion() {
		return version;
	}

	public Beer makeCopy() {
		return new Beer(this);
	}

	/**
	 * When this was written the RequestFactory required a persist method per
	 * type. That requirement should be relaxed very soon (and may well have
	 * been already if we forget to update this comment).
	 */
	public void persist() {
		BeerListService.persist(this);
	}

	public void setStyle(BeerStyle style) {
		this.style.copyFrom(style);
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setId(String id) {
		this.id = id;
		style.setId(id);
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public String getStyleName() {
		return this.style.getName();
	}

	@Override
	public String toString() {
		return "Beer [description=" + description + ", id=" + id + ", name=" + name + ", version=" + version + "]";
	}
}
